package com.example.demo.service.impl;

import com.example.demo.dao.BookMapper;
import com.example.demo.model.Book;
import com.example.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BookServiceImpl implements BookService {

  @Autowired
  private BookMapper bookMapper;

  @Override
  public void save(Book book) {
    bookMapper.insertSelective(book);
  }

  @Override
  public void update(Book book) {
    bookMapper.updateByPrimaryKeySelective(book);
  }

  @Override
  public void delete(Integer id) {
    bookMapper.deleteByPrimaryKey(id);
  }

  @Override
  public Book getDetail(Integer id) {
    return bookMapper.selectByPrimaryKey(id);
  }

  @Override
  public Object getList(String name, Integer currentPage, Integer limit) {

    Map<String, Object> map = new HashMap<>();
    int offset = (currentPage - 1) * limit;
    List<Book> productList = bookMapper.getListByParams(name, limit, offset);
    int totalCount = bookMapper.countByParams(name);
    map.put("bookList", productList);
    map.put("totalCount", totalCount);
    return map;
  }
}
